# encoding=utf-8
import psycopg2 as post
from config.yml import yml
from constant.const import const


class database(object):

    def __init__(self):
        postgres = yml().postgres
        self.database = postgres.database
        self.conn = post.connect(database=postgres.database, user=postgres.user, password=postgres.password, host=postgres.host, port=postgres.port)
        self.cur = self.conn.cursor()

    def execute(self, sql):
        try:
            self.cur.execute(sql)
            if const.select in sql:
                return self.cur.fetchall()
            else:
                self.conn.commit()
                return 1
        except Exception as e:
            print(e)

# sql2 = "update {}.book.test set id = 1111 where id = 4"
# sql3 = "select * from {}.book.test where id = 1111"
# sql1 = "select count(*) from self.book.author where author_name = '无情'"
# execute = database().execute(sql1)[0][0]
# d = database()
# execute = d.execute("select count(id) from self.book.book where url = 'https://www.bbiquge.net/book/128247/'")[0][0]
# print(execute)
